package com.topc.others.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.topc.base.exception.Asserts;
import com.topc.base.model.PageParams;
import com.topc.base.model.PageResult;
import com.topc.base.utils.PageUtils;
import com.topc.others.mapper.ArticleCommentInfoMapper;
import com.topc.others.mapper.ArticleInfoMapper;
import com.topc.others.model.dto.SaveArticleCommentDto;
import com.topc.others.model.dto.SaveArticleInfoDto;
import com.topc.others.model.po.ArticleCommentInfo;
import com.topc.others.model.po.ArticleInfo;
import com.topc.others.model.vo.ArticleCommentShowVo;
import com.topc.others.model.vo.ArticleDetailShowVo;
import com.topc.others.model.vo.ArticleShowVo;
import com.topc.others.service.IArticleCommentInfoService;
import com.topc.others.service.IArticleContentService;
import com.topc.others.service.IArticleInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 文章 服务实现类
 * </p>
 *
 * @author Gogoing
 */
@Slf4j
@Service
public class ArticleInfoServiceImpl extends ServiceImpl<ArticleInfoMapper, ArticleInfo> implements IArticleInfoService {


    @Autowired
    private IArticleContentService articleContentService;

    @Autowired
    private IArticleCommentInfoService articleCommentInfoService;

    @Autowired
    private ArticleCommentInfoMapper articleCommentInfoMapper;

    @Override
    public void saveArticleInfo(SaveArticleInfoDto saveArticleInfoDto) {
        Long contentId = articleContentService.saveContentToId(saveArticleInfoDto.getContent());
        ArticleInfo articleInfo = new ArticleInfo();
        // BeanUtil将 resource -> target 将resource的属性 赋值给target中同名的属性
        BeanUtil.copyProperties(saveArticleInfoDto, articleInfo);
        articleInfo.setContentId(contentId);
        boolean save = this.save(articleInfo);
        if (!save) {
            articleContentService.removeById(contentId);
            Asserts.fail("添加失败");
        }
    }

    @Override
    public void updateInfoById(Long id, SaveArticleInfoDto saveArticleInfoDto) {
        //修改基本信息
        LambdaUpdateWrapper<ArticleInfo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ArticleInfo::getId, id).eq(ArticleInfo::getUserNumber,saveArticleInfoDto.getUserNumber());
        updateWrapper.set(ArticleInfo::getTitle,saveArticleInfoDto.getTitle())
                .set(ArticleInfo::getAbbreviation,saveArticleInfoDto.getAbbreviation());
        boolean updateById = this.update(updateWrapper);
        if (!updateById)Asserts.fail("修改失败");
        //获取文章内容id。
        Long contentId = this.getById(id).getContentId();
        //判断文章id是否存在，通过文章id修改文章内容
        articleContentService.saveContentById(contentId,saveArticleInfoDto.getContent());
    }

    @Override
    public void deleteInfoById(Long id) {
        ArticleInfo articleInfo = this.getById(id);
        if (ObjectUtil.isEmpty(articleInfo)) Asserts.fail("删除失败");
        LambdaQueryWrapper<ArticleInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ArticleInfo::getId, id);
        boolean remove = this.remove(lambdaQueryWrapper);
        if(!remove)Asserts.fail("删除失败");
        Long contentId = articleInfo.getContentId();
        articleContentService.deleteInfoById(contentId);
    }

    /**
     *ToDo
     */
    @Override
    public PageResult<ArticleShowVo> searchByPage(PageParams pageParams, String key) {
        Page<ArticleShowVo> page = this.getBaseMapper().searchByPage(pageParams.page(), key);
        return PageUtils.pageToResult(page);
    }

    /**
     *ToDo
     */
    @Override
    public ArticleDetailShowVo searchDetailById(Long id) {
        ArticleInfo articleInfo = this.getById(id);
        Long contentId = articleInfo.getContentId();
        if (ObjectUtil.isEmpty(contentId))Asserts.fail("文章不存在");
        String content = articleContentService.getById(contentId).getContent();
        ArticleDetailShowVo articleDetailShowVo = new ArticleDetailShowVo();
        articleDetailShowVo.setAbbreviation(articleInfo.getAbbreviation());
        articleDetailShowVo.setTitle(articleInfo.getTitle());
        articleDetailShowVo.setContent(content);
        articleDetailShowVo.setUserNumber(articleInfo.getUserNumber());
        return articleDetailShowVo;
    }

    @Override
    public PageResult<ArticleCommentShowVo> searchCommentByNumber(PageParams pageParams, String userNumber) {
        Page<ArticleCommentShowVo> page = articleCommentInfoMapper.searchCommentByPage(pageParams.page(), userNumber);
        return PageUtils.pageToResult(page);
    }

    @Override
    public void saveCommentInfo(SaveArticleCommentDto saveArticleCommentDto) {
        ArticleInfo articleInfo = this.getById(saveArticleCommentDto.getArticleId());
        if(ObjectUtil.isEmpty(articleInfo))Asserts.fail("文章不存在");
        articleCommentInfoService.saveCommentInfo(saveArticleCommentDto);
    }
}
